|
1
|
|
|
/* |
|
2
|
|
|
* Copyright (c) 2017-2018 Rafael da Silva Rocha. |
|
3
|
|
|
* |
|
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
5
|
|
|
* a copy of this software and associated documentation files (the |
|
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
|
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
|
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
|
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
|
10
|
|
|
* the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be |
|
13
|
|
|
* included in all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @fileoverview The byte-data API. |
|
27
|
|
|
* @see https://github.com/rochars/byte-data |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
/** @module byteData */ |
|
31
|
|
|
|
|
32
|
|
|
import endianness from 'endianness'; |
|
33
|
|
|
import utf8BufferSize from 'utf8-buffer-size'; |
|
34
|
|
|
import {pack as packUTF8, unpack as unpackUTF8} from 'utf8-buffer'; |
|
35
|
|
|
import NumberBuffer from './lib/number-buffer.js'; |
|
36
|
|
|
import {validateValueType} from './lib/validation.js'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Read a string of UTF-8 characters from a byte buffer. |
|
40
|
|
|
* @param {!Uint8Array|!Array<number>} buffer A byte buffer. |
|
41
|
|
|
* @param {number=} index The buffer index to start reading. |
|
42
|
|
|
* @param {?number=} end The buffer index to stop reading. |
|
43
|
|
|
* If end is null will read until the end of the buffer. |
|
44
|
|
|
* @return {string} |
|
45
|
|
|
*/ |
|
46
|
|
|
export function unpackString(buffer, index=0, end=null) { |
|
47
|
|
|
return unpackUTF8(buffer, index, end); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Write a string of UTF-8 characters as a byte buffer. |
|
52
|
|
|
* @param {string} str The string to pack. |
|
53
|
|
|
* @return {!Uint8Array} The buffer with the packed string written. |
|
54
|
|
|
*/ |
|
55
|
|
|
export function packString(str) { |
|
56
|
|
|
/** @type {!Uint8Array} */ |
|
57
|
|
|
let buffer = new Uint8Array(utf8BufferSize(str)); |
|
58
|
|
|
packUTF8(str, buffer, 0); |
|
59
|
|
|
return buffer; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Write a string of UTF-8 characters to a byte buffer. |
|
64
|
|
|
* @param {string} str The string to pack. |
|
65
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
66
|
|
|
* @param {number=} index The buffer index to start writing. |
|
67
|
|
|
* Assumes zero if undefined. |
|
68
|
|
|
* @return {number} The next index to write in the buffer. |
|
69
|
|
|
*/ |
|
70
|
|
|
export function packStringTo(str, buffer, index=0) { |
|
71
|
|
|
return packUTF8(str, buffer, index); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Numbers |
|
75
|
|
|
/** |
|
76
|
|
|
* Pack a number as a byte buffer. |
|
77
|
|
|
* @param {number} value The number. |
|
78
|
|
|
* @param {!Object} theType The type definition. |
|
79
|
|
|
* @return {!Array<number>} The packed value. |
|
80
|
|
|
* @throws {Error} If the type definition is not valid. |
|
81
|
|
|
* @throws {Error} If the value is not valid. |
|
82
|
|
|
*/ |
|
83
|
|
|
export function pack(value, theType) { |
|
84
|
|
|
/** @type {!Array<number>} */ |
|
85
|
|
|
let output = []; |
|
86
|
|
|
packTo(value, theType, output); |
|
87
|
|
|
return output; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Pack a number to a byte buffer. |
|
92
|
|
|
* @param {number} value The value. |
|
93
|
|
|
* @param {!Object} theType The type definition. |
|
94
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
95
|
|
|
* @param {number=} index The buffer index to write. Assumes 0 if undefined. |
|
96
|
|
|
* @return {number} The next index to write. |
|
97
|
|
|
* @throws {Error} If the type definition is not valid. |
|
98
|
|
|
* @throws {Error} If the value is not valid. |
|
99
|
|
|
*/ |
|
100
|
|
|
export function packTo(value, theType, buffer, index=0) { |
|
101
|
|
|
return packArrayTo([value], theType, buffer, index); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Pack an array of numbers as a byte buffer. |
|
106
|
|
|
* @param {!Array<number>|!TypedArray} values The values. |
|
107
|
|
|
* @param {!Object} theType The type definition. |
|
108
|
|
|
* @return {!Array<number>} The packed values. |
|
109
|
|
|
* @throws {Error} If the type definition is not valid. |
|
110
|
|
|
* @throws {Error} If any of the values are not valid. |
|
111
|
|
|
*/ |
|
112
|
|
|
export function packArray(values, theType) { |
|
113
|
|
|
/** @type {!Array<number>} */ |
|
114
|
|
|
let output = []; |
|
115
|
|
|
packArrayTo(values, theType, output); |
|
116
|
|
|
return output; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Pack a array of numbers to a byte buffer. |
|
121
|
|
|
* @param {!Array<number>|!TypedArray} values The value. |
|
122
|
|
|
* @param {!Object} theType The type definition. |
|
123
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
124
|
|
|
* @param {number=} index The buffer index to start writing. |
|
125
|
|
|
* Assumes zero if undefined. |
|
126
|
|
|
* @return {number} The next index to write. |
|
127
|
|
|
* @throws {Error} If the type definition is not valid. |
|
128
|
|
|
* @throws {Error} If the value is not valid. |
|
129
|
|
|
*/ |
|
130
|
|
|
export function packArrayTo(values, theType, buffer, index=0) { |
|
131
|
|
|
/** @type {NumberBuffer} */ |
|
132
|
|
|
let packer = new NumberBuffer(theType); |
|
133
|
|
|
let valuesLen = values.length; |
|
134
|
|
|
for (let i = 0; i < valuesLen; i++) { |
|
135
|
|
|
validateValueType(values[i]); |
|
136
|
|
|
/** @type {number} */ |
|
137
|
|
|
let len = index + packer.offset; |
|
138
|
|
|
while (index < len) { |
|
139
|
|
|
index = packer.pack(buffer, values[i], index); |
|
140
|
|
|
} |
|
141
|
|
|
if (theType.be) { |
|
142
|
|
|
endianness( |
|
143
|
|
|
buffer, packer.offset, index - packer.offset, index); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
return index; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Unpack a number from a byte buffer. |
|
151
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer. |
|
152
|
|
|
* @param {!Object} theType The type definition. |
|
153
|
|
|
* @param {number=} index The buffer index to read. Assumes zero if undefined. |
|
154
|
|
|
* @return {number} |
|
155
|
|
|
* @throws {Error} If the type definition is not valid |
|
156
|
|
|
* @throws {Error} On bad buffer length. |
|
157
|
|
|
*/ |
|
158
|
|
|
export function unpack(buffer, theType, index=0) { |
|
159
|
|
|
/** @type {NumberBuffer} */ |
|
160
|
|
|
let packer = new NumberBuffer(theType); |
|
161
|
|
|
if ((packer.offset + index) > buffer.length) { |
|
162
|
|
|
throw Error('Bad buffer length.'); |
|
163
|
|
|
} |
|
164
|
|
|
if (theType.be) { |
|
165
|
|
|
endianness(buffer, packer.offset, index, index + packer.offset); |
|
166
|
|
|
} |
|
167
|
|
|
/** @type {number} */ |
|
168
|
|
|
let value = packer.unpack(buffer, index); |
|
169
|
|
|
if (theType.be) { |
|
170
|
|
|
endianness(buffer, packer.offset, index, index + packer.offset); |
|
171
|
|
|
} |
|
172
|
|
|
return value; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Unpack an array of numbers from a byte buffer. |
|
177
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer. |
|
178
|
|
|
* @param {!Object} theType The type definition. |
|
179
|
|
|
* @param {number=} index The buffer index to start reading. |
|
180
|
|
|
* Assumes zero if undefined. |
|
181
|
|
|
* @param {number=} end The buffer index to stop reading. |
|
182
|
|
|
* Assumes the buffer length if undefined. |
|
183
|
|
|
* @return {!Array<number>} |
|
184
|
|
|
* @throws {Error} If the type definition is not valid |
|
185
|
|
|
*/ |
|
186
|
|
|
export function unpackArray(buffer, theType, index=0, end=buffer.length) { |
|
187
|
|
|
/** @type {!Array<number>} */ |
|
188
|
|
|
let output = []; |
|
189
|
|
|
unpackArrayTo(buffer, theType, output, index, end); |
|
190
|
|
|
return output; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Unpack a array of numbers to a typed array. |
|
195
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer. |
|
196
|
|
|
* @param {!Object} theType The type definition. |
|
197
|
|
|
* @param {!TypedArray|!Array<number>} output The output array. |
|
198
|
|
|
* @param {number=} index The buffer index to start reading. |
|
199
|
|
|
* Assumes zero if undefined. |
|
200
|
|
|
* @param {number=} end The buffer index to stop reading. |
|
201
|
|
|
* Assumes the buffer length if undefined. |
|
202
|
|
|
* @throws {Error} If the type definition is not valid |
|
203
|
|
|
*/ |
|
204
|
|
|
export function unpackArrayTo( |
|
205
|
|
|
buffer, theType, output, index=0, end=buffer.length) { |
|
206
|
|
|
/** @type {NumberBuffer} */ |
|
207
|
|
|
let packer = new NumberBuffer(theType); |
|
208
|
|
|
/** @type {number} */ |
|
209
|
|
|
let originalIndex = index; |
|
210
|
|
|
while ((end - index) % packer.offset) { |
|
211
|
|
|
end--; |
|
212
|
|
|
} |
|
213
|
|
|
if (theType.be) { |
|
214
|
|
|
endianness(buffer, packer.offset, index, end); |
|
215
|
|
|
} |
|
216
|
|
|
for (let i = 0; index < end; index += packer.offset) { |
|
|
|
|
|
|
217
|
|
|
output[i] = packer.unpack(buffer, index); |
|
218
|
|
|
i++; |
|
219
|
|
|
} |
|
220
|
|
|
if (theType.be) { |
|
221
|
|
|
endianness(buffer, packer.offset, originalIndex, end); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|